﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data;
using System.Text.RegularExpressions;

public partial class qlnhanvien : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Load_data();
        }
    }
    //=============================================================================================
    protected void Load_data()
    {
        string con = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection cn = new SqlConnection(con);
        cn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        cn.Open();
        string str = "select * from NhanVien";
        SqlCommand cmd = new SqlCommand(str, cn);
        cmd.CommandText = str;
        cmd.Connection = cn;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        cn.Close();
        Repeater1.DataSource = ds;
        Repeater1.DataBind();
    }
    protected void btluunv_Click(object sender, EventArgs e)
    {
        try
        {
            lbthongbao.Text = "";
            String hoten = txttennv.Text;
            String Dienthoai = txtsodt.Text;
            if (Dienthoai.Length < 10)
            {
                lbthongbao.Text = "so dien thoai khong dung!";
            }
            else
            {
                String Diachi = txtdiachi.Text;
                int Gioitinh = int.Parse(rdlGioitinh.SelectedItem.Value);
               
                String TenDN = txttendn.Text;
                String Matkhau = txtmatkhau.Text;
                String Mapq=txtmapq.Text;
                if (Matkhau.Length < 6)
                {
                    lbthongbao.Text = "Mat khau phai >= 6 ky tu!";
                }
                else
                {
                    String Ngaysinh = ddlThangsinh.Text + "/" + ddlNgaysinh.Text + "/" + txtNam.Text;
                    String str = "INSERT INTO NhanVien(tennv,gioitinh,ngaysinh,diachi,sodt,tendn,matkhau,mapq) VALUES(N'" + hoten + "','" + Gioitinh + "','" + Ngaysinh + "',N'" + Diachi + "','" + Dienthoai + "',N'" + TenDN + "',N'" + Matkhau + "','"+ Mapq +"')";
                    XLDL.thuchienlenh(str);
                    PanelThongbao.Visible = true;
                }
            }
        }
        catch
        {
            lbthongbao.Text = "Thất bại!";
        }
    }
    protected void btxoa_Click(object sender, EventArgs e)
    {
        txttennv.Text = " ";
        txtdiachi.Text = " ";
        txtsodt.Text = " ";
        txttendn.Text = " ";
        txtmatkhau.Text = " ";
        txtNam.Text = " ";
        txtmapq.Text = " ";
    }
    protected void rept_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string con = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection cn = new SqlConnection(con);
        cn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        cn.Open();
        if (e.CommandName == "Sua")
        {
            ((Label)e.Item.FindControl("lbltennv")).Visible = false;
            ((Label)e.Item.FindControl("lblgioitinh")).Visible = false;
            ((Label)e.Item.FindControl("lblngaysinh")).Visible = false;
            ((Label)e.Item.FindControl("lbldiachi")).Visible = false;
            ((Label)e.Item.FindControl("lblsodt")).Visible = false;
            ((Label)e.Item.FindControl("lbltendn")).Visible = false;
            ((Label)e.Item.FindControl("lblmatkhau")).Visible = false;
            ((Label)e.Item.FindControl("lblmapq")).Visible = false;
            ((TextBox)e.Item.FindControl("txttennv")).Visible = true;
            ((TextBox)e.Item.FindControl("txtgioitinh")).Visible = true;
            ((TextBox)e.Item.FindControl("txtngaysinh")).Visible = true;
            ((TextBox)e.Item.FindControl("txtdiachi")).Visible = true;
            ((TextBox)e.Item.FindControl("txtsodt")).Visible = true;
            ((TextBox)e.Item.FindControl("txttendn")).Visible = true;
            ((TextBox)e.Item.FindControl("txtmatkhau")).Visible = true;
            ((TextBox)e.Item.FindControl("txtmapq")).Visible = true;
            ((LinkButton)e.Item.FindControl("LinkSua")).Visible = false;
            ((LinkButton)e.Item.FindControl("Button1")).Visible = false;
            ((LinkButton)e.Item.FindControl("LinkLuu")).Visible = true;
            ((LinkButton)e.Item.FindControl("LinkDung")).Visible = true;
        }
        if (e.CommandName == "Luu")
        {
            string tennv = ((TextBox)e.Item.FindControl("txttennv")).Text;
            string gioitinh = ((TextBox)e.Item.FindControl("txtgioitinh")).Text;
            string ngaysinh = ((TextBox)e.Item.FindControl("txtngaysinh")).Text;
            string diachi = ((TextBox)e.Item.FindControl("txtdiachi")).Text;
            string sodt = ((TextBox)e.Item.FindControl("txtsodt")).Text;
            string tendn = ((TextBox)e.Item.FindControl("txttendn")).Text;
            string matkhau = ((TextBox)e.Item.FindControl("txtmatkhau")).Text;
            string mapq = ((TextBox)e.Item.FindControl("txtmapq")).Text;
            string sql = "Update NhanVien set tennv= @tennv, gioitinh=@gioitinh, ngaysinh=@ngaysinh, diachi=@diachi, sodt=@sodt ,tendn=@tendn, matkhau=@matkhau, mapq=@mapq where manv = @manv";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.CommandText = sql;
            cmd.Connection = cn;
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            //SqlDataAdapter adp = new SqlDataAdapter("Update NhanVien set TenNhanVien= @TenNhanVien, NgaySinh=@NgaySinh,Email=@Email,DienThoai=@DienThoai ,Password=@Password where IDNhanVien = @IDNhanVien", cn);
            adp.SelectCommand.Parameters.AddWithValue("@tennv", tennv);
            adp.SelectCommand.Parameters.AddWithValue("@gioitinh", gioitinh);
            adp.SelectCommand.Parameters.AddWithValue("@ngaysinh", ngaysinh);
            adp.SelectCommand.Parameters.AddWithValue("@diachi", diachi);
            adp.SelectCommand.Parameters.AddWithValue("@sodt", sodt);
            adp.SelectCommand.Parameters.AddWithValue("@tendn", tendn);
            adp.SelectCommand.Parameters.AddWithValue("@matkhau", matkhau);
            adp.SelectCommand.Parameters.AddWithValue("@mapq", mapq);
            adp.SelectCommand.Parameters.AddWithValue("@manv", e.CommandArgument);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            Load_data();
        }
        if (e.CommandName == "Dung")
        {
            ((Label)e.Item.FindControl("lbltennv")).Visible = true;
            ((Label)e.Item.FindControl("lblgioitinh")).Visible = true;
            ((Label)e.Item.FindControl("lblngaysinh")).Visible = true;
            ((Label)e.Item.FindControl("lbldiachi")).Visible = true;
            ((Label)e.Item.FindControl("lblsodt")).Visible = true;
            ((Label)e.Item.FindControl("lbltendn")).Visible = true;
            ((Label)e.Item.FindControl("lblmatkhau")).Visible = true;
            ((Label)e.Item.FindControl("lblmapq")).Visible = true;
            ((TextBox)e.Item.FindControl("txttennv")).Visible = false;
            ((TextBox)e.Item.FindControl("txtgioitinh")).Visible = false;
            ((TextBox)e.Item.FindControl("txtngaysinh")).Visible = false;
            ((TextBox)e.Item.FindControl("txtdiachi")).Visible = false;
            ((TextBox)e.Item.FindControl("txtsodt")).Visible = false;
            ((TextBox)e.Item.FindControl("txttendn")).Visible = false;
            ((TextBox)e.Item.FindControl("txtmatkhau")).Visible = false;
            ((TextBox)e.Item.FindControl("txtmapq")).Visible = false;
            ((LinkButton)e.Item.FindControl("LinkSua")).Visible = true;
            ((LinkButton)e.Item.FindControl("Button1")).Visible = true;
            ((LinkButton)e.Item.FindControl("LinkLuu")).Visible = false;
            ((LinkButton)e.Item.FindControl("LinkDung")).Visible = false;
        }
        if (e.CommandName == "Xoa")
        {
            if (cn.State == ConnectionState.Closed)
            {
                cn.Open();
            }
            string sql = "delete from NhanVien where manv=@manv";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.Parameters.AddWithValue("@manv", e.CommandArgument);// dung hàm  CommandArgument moi lay dc con truyen la k lay dc
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            Load_data();
        }
        cn.Close();
    }
}